package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setVerifyCode(String phone, String code) {
        //存用户的手机验证码，五分钟后失效
//        String key = "verify_code:"+phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        redisTemplate.opsForValue().set(key, code, RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);


    }

    @Override
    public String getVerifyCode(String phone) {
        String key = RedisKeys.VERIFY_CODE.join(phone);
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public String setToken(UserInfo user) {
        // 创建令牌
        String token = UUID.randomUUID().toString().replaceAll("-","");
        // 以token作为key  user对象为value缓存到redis中 并设置时间为30 分钟
        String key = RedisKeys.LOGIN_TOKEN.join(token);
        redisTemplate.opsForValue().set(key,JSON.toJSONString(user),RedisKeys.VERIFY_CODE.getTime(),TimeUnit.SECONDS);
        System.out.println(token);
        return token;
    }

    @Override
    public Long getCodeTime(String phone) {
        Long time = redisTemplate.getExpire(phone);
        return time;
    }

    @Override
    public UserInfo findUserInfoByToken(String token) {
        String key = RedisKeys.LOGIN_TOKEN.join(token);
        // 如果key不存在
        if(!StringUtils.hasLength(key)){
            return null;
        }
        // key存在
        if(redisTemplate.hasKey(key)){
            String userStr = redisTemplate.opsForValue().get(key);
            //
            if(StringUtils.hasLength(userStr)) {
                // JSON.parseObject，是将Json字符串转化为相应的对象
                UserInfo userInfo = JSON.parseObject(userStr, UserInfo.class);
                //System.out.println("----------------------------------");
                // 重置token为30分钟
                redisTemplate.expire(userStr, RedisKeys.LOGIN_TOKEN.getTime(), TimeUnit.SECONDS);
                return userInfo;
            }
        }
        return null;
    }

    @Override
    public List<String> queryByStrategyFavorList(String userId) {
        // 拼接key
        String keyList = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        //判断key是否存在，存在直接获取，不存在重新new一个集合
        List<String> sidList = new ArrayList<>();
        if(redisTemplate.hasKey(keyList)){
            String listStr = redisTemplate.opsForValue().get(keyList);
            sidList = JSON.parseArray(listStr,String.class);
        }
        return sidList;
    }


}
